<?php
class QuestionsController extends AppController {

	var $name = 'Questions';
	var $uses = array('Question','Batch','Part');
	function admin_index($id=null) {
		if($id==null) $this->redirect('/admin/batches/index');        
		$this->Question->recursive = 0;
		$part = $this->Part->findByIdAndStatus($id,'active');		
		$this->paginate['Question'] = array('conditions'=>array('Question.part_id'=>$id));		
		$questions = $this->paginate('Question');
		if(count($questions)==0){
			//$this->redirect('/admin/questions/add/'.$id);
		}
		
		$this->set('questions', $questions);		
		$this->set('part', $part);
	}

	function admin_add($id=null) {
	
		if($id==null) $this->redirect('/admin/batches/index');
		$questions = $this->Question->findAllByPartId($id);
		$part = $this->Part->findById($id);
        		
		if(count($questions)>0 && $questions[0]['Question']['type'] == 'essay')
		{
			$this->Session->setFlash('You\'ve already added essay type question to this part.');
			$this->redirect('/admin/parts/index/'.$part['Part']['batch_id']);
		}		
		
		if (!empty($this->data)) {
			$this->Question->create();
			$this->data['Question']['options'] = var_export($this->data['Question']['options'],true);
			if ($this->Question->save($this->data)) {
				$this->Session->setFlash(__('The question has been saved', true));
				//$this->redirect(array('action' => 'index'));
                $this->redirect('/admin/questions/index/'.$part['Part']['id']);
			} else {
				$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
			}
		}
		$parts = $this->Question->Part->find('list');
		$this->set(compact('parts'));
        $this->set('part',$part);
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {		      
			$this->Session->setFlash(__('Invalid question', true));
			$this->redirect(array('action' => 'index'));
		}        
		if (!empty($this->data)) {
			$this->data['Question']['options'] = var_export($this->data['Question']['options'],true);
			if ($this->Question->save($this->data)) {
				$this->Session->setFlash(__('The question has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Question->read(null, $id);            
		}
        $part = $this->Part->read(null,$this->data['Question']['part_id']);
        $this->set('part',$part);
		$parts = $this->Question->Part->find('list');
		$this->set(compact('parts'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for question', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Question->delete($id)) {
			$this->Session->setFlash(__('Question deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Question was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
